package com.canbot.u05.activity.admin

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.Log
import android.view.View
import com.alibaba.fastjson.JSON
import com.canbot.u05.MainApplication
import com.canbot.u05.R
import com.canbot.u05.activity.BaseActivityNoTitle
import com.canbot.u05.activity.customvoice.tool.RecyclerItemDecoration
import com.canbot.u05.activity.logd
import com.canbot.u05.activity.loge
import com.canbot.u05.adapter.AdminFaceChooseAdapter
import com.canbot.u05.bean.PersonData
import com.canbot.u05.socket.IpMsgConst
import com.canbot.u05.utils.OkHttpUtils
import kotlinx.android.synthetic.main.activity_admin_choose_face.*
import java.util.*

/**
 * desc: 管理员人脸选择界面
 * author: xxl
 * date: 2019/6/19
 */
class AdminFaceChooseActivity : BaseActivityNoTitle(), AdminFaceChooseAdapter.OnItemClickListener {
    private lateinit var adminFaceChooseAdapter: AdminFaceChooseAdapter
    private var personDatas: List<PersonData> = ArrayList()
    private var groupId: Int = 0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        groupId = intent.getIntExtra("groupId", 1)
        setContentView(R.layout.activity_admin_choose_face)
        val groupList = ArrayList<PersonData>()
        val layoutManager = GridLayoutManager(this, 6)
        recycle_face_choose.layoutManager = layoutManager
        recycle_face_choose.addItemDecoration(RecyclerItemDecoration(6, resources.getDimension(R.dimen.px22).toInt(), resources.getDimension(R.dimen.px20).toInt(), false))
        adminFaceChooseAdapter = AdminFaceChooseAdapter(this, groupList)
        adminFaceChooseAdapter.setItemClickListener(this)
        recycle_face_choose.adapter = adminFaceChooseAdapter
        getData()

        et_search_face.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(seq: CharSequence, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(seq: CharSequence, start: Int, before: Int, count: Int) {}

            override fun afterTextChanged(editable: Editable) {
                val searchWord = et_search_face.text.toString()
                updateFaceUi(searchWord)
            }
        })
    }

    /**
     * 通过用户输入的关键字，更新人脸列表界面
     * @param searchWord
     */
    private fun updateFaceUi(searchWord: String) {

        if (personDatas.isEmpty()) { //人脸数据库为空时，为空界面展示
            no_results_found_container.visibility = View.VISIBLE
            tv_no_results_were_found.text = getString(R.string.no_face_data)
            return
        }

        no_results_found_container.visibility = View.GONE

        //关键字为空时展示所有人脸数据
        if (TextUtils.isEmpty(searchWord)) {
            adminFaceChooseAdapter.setPersonList(personDatas)
            return
        }

        val searchDatas = ArrayList<PersonData>()

        //获取所有带关键字的人脸
        for (person in personDatas) {
            if (person.name.contains(searchWord)) {
                searchDatas.add(person)
            }
        }

        //搜索结果为空时界面展示
        if (searchDatas.isEmpty()) {
            no_results_found_container.visibility = View.VISIBLE
            val string = resources.getString(R.string.no_results_were_found)
            val sFinal1 = String.format(string, searchWord)
            tv_no_results_were_found.text = sFinal1

        } else {
            no_results_found_container.visibility = View.GONE
        }

        //展示搜索结果
        adminFaceChooseAdapter.setPersonList(searchDatas)
    }

    /**
     * 获取对应分组内的人脸数据
     */
    private fun getData() {
        val hashMap = HashMap<String, Any>()
        loge("groupId:$groupId")
        hashMap["id"] = groupId.toString() + ""
        OkHttpUtils.upLoadFile("http://" + IpMsgConst.CHEST_SOCKET_HOST + ":7755/" + "getfacerecginfo", hashMap, object : OkHttpUtils.OkHttpCallBack {
            override fun onSuccess(result: String) {
                loge("onSuccess")
                personDatas = JSON.parseArray(result, PersonData::class.java)
                loge("personDatas:" + personDatas.size)

                runOnUiThread { updateFaceUi("") }
            }

            override fun onFailure() {
                loge("获取人脸数据失败")
                updateFaceUi("")
            }
        })

    }

    fun back(view: View) = finish()


    override fun onItemClick(personData: PersonData) {
        val intent = Intent()
        val jsonString = JSON.toJSONString(personData)
        intent.putExtra("personData", jsonString)
        setResult(AdminFaceModifyActivity.RESULT_OK, intent) //返回选中的人脸数据
        finish()
    }
}
